<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class administrator extends Controller {
    
	function Administrator()
	{
		parent::Controller();	
		$this->load->model("models");
        $this->load->model("version");
		$this->load->model("complete");
	    $this->load->model("category");
	    $this->load->model("company");
	    $this->load->model("receive");
	    $this->load->model("second_send");
	    $this->load->model("send");
	    $this->load->model("historys");
	    $this->load->model("update_historys");
	    $this->load->model("inventory");
		$this->load->model("admin");
		$this->load->model("historys");
		$this->load->model("version");
	}


	function export(){
		$mod_id=$_POST['export_mod'];
	    $export=$_POST['export'];

	   if($export=="下载模板"){ 
		       require_once APPPATH.'libraries/PHPExcel/PHPExcel.php';
		       require_once APPPATH.'libraries/PHPExcel/PHPExcel/IOFactory.php';
		       $objPHPExcel = new PHPExcel();
		       $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
								->setLastModifiedBy("Maarten Balliauw")
								->setTitle("Office 2007 XLSX Test Document")
								->setSubject("Office 2007 XLSX Test Document")
								->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
								->setKeywords("office 2007 openxml php")
								->setCategory("Test result file");
			    
				$category = $this->category->select_export($mod_id);
		
				$objPHPExcel->setActiveSheetIndex(0);
				$objRichText = new PHPExcel_RichText();
				$objRichText->createText('');
				$objPayable = $objRichText->createTextRun('PHP导出的Excel');

				$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
				$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18);
				$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(100);
				$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
				$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
				$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(18);
				for($i = 2; $i <= 17; $i++)
				{
				$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(22);
				}
				$i=2;
		            $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
					$objPHPExcel->getActiveSheet()->setCellValue('B1', '型号');
					$objPHPExcel->getActiveSheet()->setCellValue('C1', '资料名称');
					$objPHPExcel->getActiveSheet()->setCellValue('D1', '资料编号');
					$objPHPExcel->getActiveSheet()->setCellValue('E1', '资料密级');
					$objPHPExcel->getActiveSheet()->setCellValue('F1', '创建时间');		
					$level = array('机密','秘密','内部');
				foreach($category as $k=>$val){
		            if($val->createtime==""){
		               $time = date('Y-m-d');
		            }else{
		               $time = date('Y-m-d',$val->createtime);
		            }
					$objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $val->category_id);
					$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $val->mod_name);
					$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $val->name);
					$objPHPExcel->getActiveSheet()->setCellValueExplicit('D'.$i, $val->codes,PHPExcel_Cell_DataType::TYPE_STRING);
					$objPHPExcel->getActiveSheet()->setCellValue('E'.$i, $level[$val->level]);
					$objPHPExcel->getActiveSheet()->setCellValue('F'.$i, $time);
					$i++;
				}
				$objPHPExcel->setActiveSheetIndex(0);
				$objPHPExcel->getActiveSheet()->setTitle('Excel表');
				   
				$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
				$name = 'img/template_'.$mod_id.'.xls';
				$name = iconv('utf-8','gb2312',$name);
				$objWriter->save($name);	
	   }else{
	   	require_once APPPATH.'libraries/excelreader.class.php';
		    $path = "uploads/";
			$name = $_FILES['category_export']['name'];
			$path=$path.$name;
			move_uploaded_file($_FILES["category_export"]["tmp_name"],$path);
			if(is_readable($path)){
				$data = new Spreadsheet_Excel_Reader();
				$data->setOutputEncoding('UTF-8');
				$data->read($path);
				$filmarray = $data->sheets[0]['cells'];	
			}else{
				return "文件不可读";
			}
			$j=1;
			$level = array('机密'=>'0','秘密'=>'1','内部'=>'2');
			foreach($filmarray as $film){
				if($j>1){
					 
	//根据型号名，去查询型号id
	                     $mod_name = $film[2];		
	                     $mod = $this->models->select_array(array('name'=>trim($mod_name)));		 	
					     $array = array(
					                    'mod_id'=>$mod[0]->mod_id,
					                    'name'=>$film[3],
					                    'codes'=>$film[4],
					                    'level'=>$level[$film[5]],
					                    'createtime'=>strtotime($film[6])
					      );
					  $this->category->insert_array($array);    					 
				}
				$j++;
			}
			redirect('administrator/category');
	   }		
	}
	
	
	
	function getcategory(){
		  $mod_id = $_POST['mod_id'];
	      $result = $this->category->getcategory_bymod_id($mod_id);
	      $count = count($result);
	      $str="";
	      if($count!=0){
	         for($i=0;$i<$count;$i++){
	           $str.="<p style='width:190px;float:left;'><input type='checkbox' name='category_ids[]' value='".$result[$i]->category_id."' />".$result[$i]->name." </p>";
	         }
	         echo $str;
	      }else{
	        echo "暂无种类";
	      }
	}
	
	function get_inventory_for_layout(){
	    $inventory_id = $_POST['inventory_id'];
	    $inventory = $this->inventory->select_by_id($inventory_id);
	    $mod = $this->models->select($inventory[0]->mod_id);
	    $category = $this->category->select(array('category_id'=>$inventory[0]->category_id));
	    $pre_version = $this->version->getversion_array(array('version_id'=>$inventory[0]->version_id));
	    $version = $this->version->getversion_array(array('category_id'=>$inventory[0]->category_id));
	    $str = "";
	    foreach($version as $k=>$v){
	        $str.="<option value='".$v->version_id."'";
	        if($inventory[0]->version_id==$v->version_id){
	          $str.="  selected=true  ";
	        }
	        $str.=">".$v->name."</option>";
	    }
	    $array = array(
	                    'mod'=>$mod[0]->name,
	                    'category'=>$category[0]->name,
	                    'count'=>$inventory[0]->count,
	                    'version' => $str,
	                    'pre_verison'=>$pre_version[0]->name,
	                    'mod_id'=>$inventory[0]->mod_id,
	                    'category_id'=>$inventory[0]->category_id,
	                    'inventory_id'=>$inventory_id,
	                    'pre_version_id'=>$pre_version[0]->version_id,
	    );
	    $arr = json_encode($array);
	    echo $arr;
	    
	}
	
	
	function update_inventory(){
	    $inventory_id = $_POST['inventory_id'];
		$mod_id = $_POST['mod_id'];
		$category_id = $_POST['category_id'];
		$version_id = $_POST['version_id'];
		$count = $_POST['count'];
		$codes = $_POST['codes'];
		$pre_version = $_POST['pre_version'];
		$pre_version_id = $_POST['pre_version_id'];
		$pre_count = $_POST['pre_count'];	
		
		$user = $this->session->userdata('user');
		if($count>$pre_count){
		   echo '更新库存数量不能大于'.$pre_count;exit;
		}else {

		  $inven = array(
		               'mod_id'=>$mod_id,
		               'category_id'=>$category_id,
		               'version_id'=>$version_id,
		               'count'=>$count		  
		  );
		  $result = $this->inventory_manage($inven);
//新增库存后，要新增一条接收记录
//           $data = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id,'count'=>$count,'createtime'=>time(),'codes'=>$codes,'contacter'=>$user[0]->admin_id);
//           $r_id = $this->receive->insert($data);
//           $data['is_update'] = 1;
//           $data['r_id'] = $r_id;
//           $this->receive_record->insert($data);
          $c_v = $this->version->select(array('version_id'=>$version_id));
          $ar = array('mod_id'=>$mod_id,'user'=>$user[0]->admin_id,'category_id'=>$category_id,'version_id'=>$version_id,'count'=>$count,'r_codes'=>$codes,'createtime'=>time());
          $arReceiveIn = array('mod_id'=>$mod_id,'contacter'=>$user[0]->admin_id,'category_id'=>$category_id,'current_version'=>$c_v[0]->name,'version_id'=>$version_id,'count'=>$count,'codes'=>$codes,'createtime'=>time());
          $r_id = $this->receive->insert($arReceiveIn);
          $mod = $this->models->select($mod_id);
          $category = $category = $this->category->select(array('category_id'=>$ar['category_id']));
          $ar['s_r_id'] = $r_id;
          $ar['mod'] =$mod [0]->name;
          $ar['category'] =$category [0]->name;
          $version = $this->version->select(array('version_id'=>$ar['version_id']));
          $ar['version'] = $version[0]->name;
          $ar['type'] = 2;
          $ar['is_update']=0;
          $hs = $this->historys->insert($ar);
                    
		  if($result){
//更新原来的那个库存
            $in = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$pre_version_id,'count'=>'-'.$count);
            $this->inventory_manage($in);		  	
                  $arrr = array(
	                  'mod_id'=>$mod_id,
	                  'category_id'=>$category_id,
	                  'version_id'=>$version_id,
	              	  'count'=>$count,
	              	  'pre_version'=>$pre_version_id,
	              	  'codes'=> $codes	
                  );	
              $ss = $this->update_historys->insert($arrr);  
              if($ss){
              	echo '1';
              }else{
                echo '操作失败！';
              }    
		  }
		}
	
	}
	
	
	function get_receive_for_layout(){
	    $inventory_id = $_POST['inventory_id'];
	    $receive = $this->inventory->select(array('inventory_id'=>$inventory_id));
	    $mod = $this->models->select($receive[0]->mod_id);
	    $category = $this->category->select(array('category_id'=>$receive[0]->category_id));
	    $version = $this->version->getversion_array(array('category_id'=>$receive[0]->category_id));
	    $current_version = $this->version->getversion_array(array('version_id'=>$receive[0]->version_id));
	    $str = "";
	    foreach($version as $k=>$v){
	        $str.="<option value='".$v->version_id."'";
	        if($receive[0]->version_id==$v->version_id){
	          $str.="  selected=true  ";
	        }
	        $str.=">".$v->name."</option>";
	    }
	    $array = array(
	                    'mod'=>$mod[0]->name,
	                    'category'=>$category[0]->name,
	                    'version' => $str,
	                    'current_version'=>$current_version[0]->name,
	                    'count'=>$receive[0]->count,
	                    'pre_verison'=>$receive[0]->version_id,
	                    'mod_id'=>$receive[0]->mod_id,
	                    'category_id'=>$receive[0]->category_id,
	                    'inventory_id'=>$inventory_id
	    );
	    $arr = json_encode($array);
	    echo $arr;
	    
	}
	
	
	function update_receive(){
		//只增加一条更新记录
	    $inventory_id = $_POST['receive_id'];
		$mod_id = $_POST['mod_id'];
		$category_id = $_POST['category_id'];
		$version_id = $_POST['version_id'];
		$count = $_POST['count'];
		$codes = $_POST['codes'];
		$pre_version = $_POST['pre_version'];
		$pre_count = $_POST['pre_count'];
		$pre_count_record = $_POST['pre_count_record'];
		$user = $this->session->userdata('user');

        if($version_id != $pre_version){
    //版次发生变化,先判断是否已存在更改后的版次，如果已经存在，则首页合并，后台新增一条接收数据，同时更改库存
              $arr = array('mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id'],'version_id'=>$_POST['version_id']);
              $result = $this->inventory->select_by_array($arr); 
              $count_record = count($result);
              if($count_record>0){
                 $c = $result[0]->count+$count;
                 $re = $this->inventory->update($result[0]->inventory_id,array('count'=>$c));
                 if($re){
                 	$this->inventory->del($inventory_id);
                 }
              }else{
                 $this->inventory->update($inventory_id,array('version_id'=>$_POST['version_id']));
              }
                  $arrrr = array('company_id'=>'0','mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id'],'version_id'=>$_POST['version_id'],'updatetime'=>$_POST['updatetime']);
              	  $arrrr['count']=$count;

              	  $arrrr['pre_version'] = $pre_version;
              	  $arrrr['codes'] = $codes;
              	  $arrrr['createtime'] = time();
              	  $this->update_historys->insert($arrrr);

        }
        echo '1';exit();	
	}
	
	
	function update_send(){
	//更新首页版次，所有数量，只在更新记录里增加一条记录。
	    $send_id = $_POST['send_id'];
		$mod_id = $_POST['mod_id'];
		$category_id = $_POST['category_id'];
		$version_id = $_POST['version_id'];
		$company_id = $_POST['company_id'];
		$count = $_POST['count'];
		$pre_version = $_POST['pre_version'];
		$pre_count = $_POST['pre_count'];
		$des = $_POST['des'];
		$codes = $_POST['codes'];
		$sendtime = $_POST['sendtime'];
		$user = $this->session->userdata('user');

     if($pre_version!=$version_id){
    //根据新传过来的数据，先判断是否已经给这个公司发放过新版本资料，发放时间也是条件，如果已发放过，则数量累加，如果没有，则新增记录  		
	      $arr = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id,'company_id'=>$company_id,'sendtime'=>$sendtime); 
	      $arra = $this->send->select_by_array($arr);
	     
	      $count_record = count($arra);
	      if($count_record!=0){
                   $c = $arra[0]->count+$count;
                   $r = $this->send->update($arra[0]->send_id,array('count'=>$c));
                   $s_id = $arra[0]->send_id;
           }else{
	          //不存在发放记录		      
	               $cc = $pre_count-$count;
	               $r = $this->send->update($send_id,array('count'=>$cc)); 
	               $arr['count'] = $count;
	               $this->send->insert($arr);	  
          }
          unset($arr['sendtime']);
     	  $arr['count'] = $count;
          $arr['pre_version'] = $pre_version;
     	  $arr['codes'] = $_POST['codes'];
          $arr['updatetime'] = $_POST['updatetime'];
          $arr['createtime'] = time();
          $this->update_historys->insert($arr);
	    
	      echo '1';	   
	   }
	//版次数量都不同
	}
	
	
	function get_send_for_layout(){
	    $send_id = $_POST['send_id'];
	    $send = $this->send->select($send_id);
	    $mod = $this->models->select($send[0]->mod_id);
	    $category = $this->category->select(array('category_id'=>$send[0]->category_id));
	    $version = $this->version->getversion_array(array('category_id'=>$send[0]->category_id));
	    $current_version = $this->version->getversion_array(array('version_id'=>$send[0]->version_id));
	    $com = $this->company->select($send[0]->company_id);
	    $str = "";
	    foreach($version as $k=>$v){
	        $str.="<option value='".$v->version_id."'";
	        if($send[0]->version_id==$v->version_id){
	          $str.="  selected=true  ";
	        }
	        $str.=">".$v->name."</option>";
	    }
	    $array = array(
	                    'mod'=>$mod[0]->name,
	                    'category'=>$category[0]->name,
	                    'version' => $str,
	                    'current_version'=>$current_version[0]->name,
	                    'count'=>$send[0]->count,
	                    'pre_verison'=>$send[0]->version_id,
	                    'des'=>$send[0]->des,
	                    'mod_id'=>$send[0]->mod_id,
	                    'category_id'=>$send[0]->category_id,
	                    'send_id'=>$send_id,
	                    'codes'=>$send[0]->codes,
	                    'company_id'=>$send[0]->company_id,
	                    'user_id'=>$send[0]->user_id,
	    		        'company'=>$com[0]->name,
	    		        'sendtime'=>$send[0]->sendtime
	    );
	    $arr = json_encode($array);
	    echo $arr;
	    
	}

	function second_send(){
		//首页更改，如有相同，首页合并，资料发放2条，一正，一负
		$send_id = $_POST['send_id'];
		$mod_id = $_POST['mod_id'];
		$category_id = $_POST['category_id'];
		$version_id = $_POST['version_id'];
		$mod = $_POST['mod'];
		$category = $_POST['category'];
		$version = $_POST['version'];
		$pre_company = $_POST['pre_company'];
		$next_company = $_POST['next_company'];
		$count = $_POST['count'];
		$pre_count = $_POST['pre_count'];
		$des = $_POST['des'];
		$codes = $_POST['codes'];
		$pre_codes = $_POST['pre_codes'];
		$user = $_POST['user'];
		$updatetime = $_POST['updatetime'];
		//如果二次分发份数等于原来分发数量，则删除原来分发记录，从新发送
		if($pre_count==$count){
			$this->send->del($send_id);
			$this->send->update($send_id,array('company_id'=>$next_company));
			
			$s_id=$send_id;
		}else{
		//更新原来的那个
			$b['count'] = $pre_count-$count;
			
			$resu =$this->send->update($send_id,$b);
			
			$cond = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id,'company_id'=>$next_company,'sendtime'=>$updatetime);
			$send_re = $this->send->sel2($cond);
			if(count($send_re)>0){
				$s_id = $send_re[0]->send_id;
				$cou = $send_re[0]->count+$count;
				$this->send->update($s_id,array('count'=>$cou));
			}else{
				$cond['count'] = $count;
				$cond['codes'] = $codes;
				$cond['createtime'] = time();
				$cond['des'] = '从'.$_POST['current_company'].'二次分发而来';
				$s_id = $this->send->insert($cond);
			}
		}
//历史记录，发给第一个公司 负数，第二个公司正数		
		$h['count'] = '-'.$count;
		$h['user']=$user;
		$h['s_codes']=$codes;
		$h['mod']=$mod;
		$h['category']=$category;
		$h['version']=$version;
		$h['mod_id']=$mod_id;
		$h['category_id']=$category_id;
		$h['version_id']=$version_id;
		$h['createtime']=time();
		$h['sendtime'] = $updatetime;
		$h['is_update']=0;
		$h['s_r_id'] = $send_id;
		$h['company_id'] = $pre_company;
		$h['type']=1;
		
		$this->historys->insert($h);
		
		$h['s_r_id'] = $s_id;		
		$h['count'] = $count;
		$h['des'] = '从'.$_POST['current_company'].'二次分发而来';
		$h['company_id'] = $next_company;
		$this->historys->insert($h);
		echo '1';exit;
	}
	

	function get_secondsend_data(){
	  $send_id = $_POST['send_id'];
	    $send = $this->send->select($send_id);
	    $mod = $this->models->select($send[0]->mod_id);
	    $category = $this->category->select(array('category_id'=>$send[0]->category_id));
	    $version = $this->version->getversion_array(array('category_id'=>$send[0]->category_id));
	    $current_version = $this->version->getversion_array(array('version_id'=>$send[0]->version_id));
	    $company = $this->company->select($send[0]->company_id);
	    $compays = $this->company->select('');
	    $str = "";
	    foreach($compays as $k=>$v){
	    	if($v->company_id!=$send[0]->company_id){
		        $str.="<option value='".$v->company_id."'";
		        $str.=">".$v->name."</option>";
	    	}
	    }
	    $array = array(
	                    'mod'=>$mod[0]->name,
	                    'category'=>$category[0]->name,
	                    'version' => $str,
	                    'current_version'=>$current_version[0]->name,
	                    'count'=>$send[0]->count,
	                    'pre_verison'=>$send[0]->version_id,
	                    'des'=>$send[0]->des,
	                    'mod_id'=>$send[0]->mod_id,
	                    'category_id'=>$send[0]->category_id,
	                    'send_id'=>$send_id,
	                    'codes'=>$send[0]->codes,
	                    'company_id'=>$send[0]->company_id,
	    		        'current_company'=>$company[0]->name,
	    		        'companys' =>$str,
	                    'user_id'=>$send[0]->user_id
	    );
	    $arr = json_encode($array);
	    echo $arr;	
	}

//用户管理列表
	function admin(){
		$this->top();
	    $total = $this->admin->select('');
	    $config['base_url'] =  site_url('administrator/admin/');
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start'] = $start;
		$data['admin'] = $this->admin->select('',$config['per_page'],$page);
		$this->load->view('admin',$data);
		
	}
	
	function update_historys(){
		$this->top();
		if(isset($_POST['mod_id'])){
			$this->session->set_userdata('send_mod_id',$_POST['mod_id']);
			$this->session->set_userdata('send_category_id',$_POST['category_id']);
			$this->session->set_userdata('send_company_id',$_POST['company_id']);
			$this->session->set_userdata('send_codes',$_POST['codes']);
		}
		$mod_id=$this->session->userdata('send_mod_id');
		$category_id=$this->session->userdata('send_category_id');
		$company_id=$this->session->userdata('send_company_id');
		$codes=$this->session->userdata('send_codes');
		$where = array();
		if($mod_id!=""){
			$where = array('mod_id'=>$mod_id);
			if($category_id!=""){
				$where['category_id'] = $category_id;
			}
			if($codes!=''){
				$where['codes'] = $codes;
			}
		}
		if($company_id!=""){
			$where['company_id'] = $company_id;
		}
		if(count($where)==0){
			$where="";
		}
		$data['mod_id'] = $mod_id;
		$data['category_id'] = $category_id;
		$data['company_id'] = $company_id;
		$data['codes'] = $codes;
		$total = $this->update_historys->select2($where);
		$config['base_url'] =  site_url('administrator/update_historys/');
		$config['total_rows'] = count($total);
		$config['per_page'] = 20;
		$config['num_links'] = 40;
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$config['full_tag_open'] = '<div>';
		$config['full_tag_close'] = '</div>';
		$this->pagination->initialize($config);
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
			$page=0;
			$start=1;
		}else{
			$start = $page+1;
		}
		$data['start'] = $start;
		$data['mod'] = $this->models->select('');
		$data['category'] = $this->category->select('');
		$data['version'] = $this->version->select('');
		$data['company'] = $this->company->select('');
		$data['update_historys'] = $this->update_historys->select2($where,$config['per_page'],$page);
		$this->load->view('update_historys',$data);
	}
	
	
    function admin_auth(){
       $username = $_POST['username'];
       $count = $this->admin->auth($username);
       echo $count;exit;
    }	

	//添加用户
	function admin_add(){
	  if($_POST){
	  	$resu = $this->admin->insert();
	  	if($resu){
	  	  redirect('administrator/admin');
	  	}
	  }else{	  	
	    $this->top();
		$this->load->view('admin_add');
	  }
	}
	
	
//重置用户密码
    function admin_reset(){
        $id = $this->uri->segment(3);
	    $resu = $this->admin->update_reset($id); 
	     if($resu){
	  	  redirect('administrator/admin');
	  	 }
    }	
	
	function admin_detail(){
	    $id = $this->uri->segment(3);
	    $admin = $this->admin->select($id);
	    $data['admin'] = $admin[0]; 
	    $this->top();
        $this->load->view('admin_detail',$data);
	 
	}

	//删除用户
	function admin_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->admin->del($id); 
	     if($resu){
	  	  redirect('administrator/admin');
	  	 }
	}

	//编辑用户
	function admin_edit(){
	  $id = $this->uri->segment(3);
	  $user = $this->session->userdata('user');
	  if($_POST){
	     $resu = $this->admin->update($id); 
	     if($resu){
	       if($user[0]->role==1){
	       	 redirect('main/index');
	       }else{
	         redirect('administrator/admin');
	       }		  	  
	  	 }
	  }else{
	     
	     $admin = $this->admin->select($id);
	     $data['admin'] = $admin[0];
	     $data['id']=$id;
	     $this->top();
	     $this->load->view('admin_add',$data);
	  }
	}


	function auth(){
	   $type = $_POST['type'];
	   
	   if($type=="mod"){
	      $result = $this->models->auth($_POST['name']);
	      
	   }else if($type=="category"){
	   	  $category_id = $_POST['category_id'];
	      $result = $this->category->auth($_POST['codes'],$_POST['mod_id'],$category_id);
	   }else if($type=="company"){
	      $result = $this->company->auth($_POST['name']);
	   }else if($type=="version"){
	      $result = $this->version->auth($_POST['name'],$_POST['category_id']);
	   }
	   echo $result;exit();
	}
	
	
	function top(){
	    $user = $this->session->userdata('user');
		if($user!=""){
			$data['user'] = $user;
		}else{ 
			//直接跳转到登录页面
			redirect('main/login');exit;
		}
	    $data['module']=$this->uri->segment(1);
	    $data['template']=$this->uri->segment(2);
	    $template = $this->uri->segment(2);
	    if($template=="index"||$template=="model_edit"||$template=="model_add"||$template=="model_detail"){
	       $data['title'] = "型号管理-用户资料管理系统";
	    }else if($template=="category"||$template=="category_edit"||$template=="category_add"||$template=="category_detail"){
	    	$data['title'] = "类别管理-用户资料管理系统";
	    }else if($template=="version"||$template=="version_edit"||$template=="version_add"||$template=="version_detail"){
	    	$data['title'] = "版本管理-用户资料管理系统";
	    }else if($template=="complete"||$template=="complete_edit"||$template=="complete_add"||$template=="complete_detail"){
	    	$data['title'] = "成套管理-用户资料管理系统";
	    }else if($template=="company"||$template=="company_edit"||$template=="company_add"||$template=="company_detail"){
	    	$data['title'] = "发放单位管理-用户资料管理系统";
	    }else if($template=="send"||$template=="send_edit"||$template=="send_add"||$template=="send_detail"){
	    	$data['title'] = "资料发放管理-用户资料管理系统";
	    }else if($template=="receive"||$template=="receive_edit"||$template=="receive_add"||$template=="receive_detail"){
	    	$data['title'] = "资料接收管理-用户资料管理系统";
	    }else if($template=="update_historys"){
	    	$data['title'] = "资料更新管理-用户资料管理系统";
	    }else if($template=="admin"||$template=="admin_edit"||$template=="admin_add"||$template=="admin_detail"){
	    	$data['title'] = "用户管理-用户资料管理系统";
	    }else if($template=="inventory"){
	    	$data['title'] = "库存管理-用户资料管理系统";
	    }
	    
	    $data['level_data'] = array('机密','秘密','内部');
	    $this->load->view('top',$data);
	}
	
	function inventory(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('inventory_mod_id',$_POST['mod_id']);
	    	$this->session->set_userdata('inventory_category_id',$_POST['category_id']);
	    	$this->session->set_userdata('inventory_codes',$_POST['codes']);
	    }
	    
	    if(isset($_POST['see'])){
	        $this->session->set_userdata('see',$_POST['see']);
	    }
	    $mod_id=$this->session->userdata('inventory_mod_id');
	    $see=$this->session->userdata('see');
	    $category_id=$this->session->userdata('inventory_category_id');
	    $codes=$this->session->userdata('inventory_codes');
	    if($mod_id!=""){
	    	$where = array('inventory.mod_id'=>$mod_id);
	    	if($category_id!=""){
	    	  $where['inventory.category_id'] = $category_id;                                                    
	    	}
	    	if($codes!=''){
	    	  $where['codes'] = $codes;
	    	}
	    }else{
	        $where = "";
	    }
	    
	    $data['see'] = $see;	    	
	    $data['mod_id'] = $mod_id;
	    $data['category_id'] = $category_id;
	    $data['codes'] = $codes;
	    
        $data['mod'] = $this->models->select('');
        $data['category'] = $this->category->select('');
        $data['version'] = $this->version->select('');
	    $total = $this->inventory->select3($where);
	    $config['base_url'] =  site_url('administrator/inventory/');
	    
		$config['total_rows'] = count($total);
		if(isset($_POST['see_all'])&&($_POST['see_all']==1)){
		  $config['per_page'] = 20000000000; 
		  $data['see_all'] = 1;
		}else{
		  $data['see_all'] = 0;	
		  $config['per_page'] = 20; 
		}
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		//取得每个种类的最新版次
		$sr = $this->version->get_last_verison(); 
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start'] = $start;
		$data['inventory'] = $this->inventory->select3($where,$config['per_page'],$page);
		$cou = 0;
		for($i=0;$i<count($data['inventory']);$i++){
			$cou+=$data['inventory'][$i]->count;
		     for($j=0;$j<count($sr);$j++){
		     	
		         if($sr[$j]['category_id']==$data['inventory'][$i]->category_id){
		           if($sr[$j]['version_id']==$data['inventory'][$i]->version_id){
		           	$data['inventory'][$i]->update=0;
		           }else{
		           	$data['inventory'][$i]->update=1;
		           }
		         }
		     }
		}
		$data['cou'] = $cou;
		$this->load->view('inventory',$data);
	}
	
	
	function get_version_detail(){
	    $version = $this->inventory->select(array('version_id'=>$_POST['version_id'],'category_id'=>$_POST['category_id']));
        if(count($version)==0){
          echo 0;
        }else{
	      echo $version[0]->count;
        }
	}
	
//型号资料列表
    function receive(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('receive_mod_id',$_POST['mod_id']);
	    	$this->session->set_userdata('receive_category_id',$_POST['category_id']);
	    	$this->session->set_userdata('receive_codes',$_POST['codes']);
	    }
	    $mod_id=$this->session->userdata('receive_mod_id');
	    $category_id=$this->session->userdata('receive_category_id');
	    $codes=$this->session->userdata('receive_codes');
	    if($mod_id!=""){
	    	if($category_id!=''){
	    	  $where = array('mod_id'=>$mod_id,'category_id'=>$category_id);
	    	}else{
	    	  $where = array('mod_id'=>$mod_id);
	    	}
	    	if($codes!=''){
	    	  $where['r_codes'] = $codes;
	    	}
	    }else{
	        $where = "";
	    }
        $where['type'] = '2';
	    $data['mod_id'] = $mod_id;
	    $data['category_id'] = $category_id;
	    $data['codes'] = $codes;
        $data['version'] = $this->version->select('');
        $data['mod'] = $this->models->select('');
        $data['category'] = $this->category->select('');
        $data['admin'] = $this->admin->select('');
	    $total = $this->historys->select($where);
	    $config['base_url'] =  site_url('administrator/receive/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['receive'] = $this->historys->select($where,$config['per_page'],$page);
		$this->load->view('receive',$data);
    }	
    
     function get_category(){
         $mod_id = $_POST['mod_id'];
         $result = $this->category->getcategory_bymod_id($mod_id);
         $str="<option value=''>全部资料种类</option>";
         foreach( $result as $k=>$val ){
         	$str.="<option value='".$val->category_id."'>".$val->name."</option>";
         }
         echo $str;
     }
     
     function get_version(){
         $category_id = $_POST['category_id'];
         $mod_id = $_POST['mod_id'];
         $result = $this->version->getversion_bycategory_id($category_id);
         $str="<option value=''>请选择版本</option>";
         foreach( $result as $k=>$val ){
         	$version = $this->inventory->select(array('version_id'=>$val->version_id,'mod_id'=>$mod_id,'category_id'=>$category_id));
         	if((count($version)!=0)&&($version[0]->count!=0)){
         	$str.="<option value='".$val->version_id."'>".$val->name."</option>";
         	}
         }
         echo $str;
     }
     
     function get_version_for_send(){
     	$category_id = $_POST['category_id'];
     	$mod_id = $_POST['mod_id'];
     	$result = $this->version->getversion_bycategory_id($category_id);
     	$str="<option value=''>请选择版本</option>";
     	foreach( $result as $k=>$val ){
     			$str.="<option value='".$val->version_id."'>".$val->name."</option>";
     	
     	}
     	echo $str;
     }
     
     
     
     function inventory_manage($array){
         $mod_id=$array['mod_id'];
         $category_id = $array['category_id'];
         $version_id = $array['version_id'];
         $count = $array['count'];
     //先根据mod_id category_id version_id 查询表,判断数据存在与否,不存在执行插入,存在,执行update
         $arrays=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id);
         $result = $this->inventory->select2($arrays);

         $num = count($result);
         if($num==0){
           $arrays['count']=$count;
           $r = $this->inventory->insert($arrays);
         }else{
      //如果存在此条数据,则拿出其count 与数组传过来的count进行累加，然后更新此条记录
           $left_count = $result[0]->count+$count;
           $arra = array('count'=>$left_count);
           if($left_count<=0){
           	$r = $this->inventory->del($result[0]->inventory_id);
           }else{
           	$r = $this->inventory->update($result[0]->inventory_id,$arra);
           }
           
         }
         return $r;
     }
     
//型号资料添加
     function receive_add(){
        if($_POST){
        	$user = $this->session->userdata('user');
        	foreach($_POST as $k=>$val){
        	    $key = explode('_', $k);
        	    if(count($key)==3){
        	    	
        	       if($key[0]=="version"){	
        	          $ver = $key[0]."_".$key[1]."_".$key[2];
        	          $count = "counts"."_".$key[1]."_".$key[2];
        	          $c = count($_POST[$ver]);
        	          for( $i=0;$i<$c;$i++){
        	           //从版本表里获取version_id
        	            // $version = array('mod_id'=>$key[1],'category_id'=>$key[2],'name'=>$_POST[$ver][$i],'count'=>$_POST[$count][$i],'createtime'=>time());	
        	            // $version_id = $this->version->get_id($version);
        	             $c_v = $this->version->select(array('version_id'=>$_POST[$ver][$i]));
        	             $ar = array('mod_id'=>$key[1],'user'=>$_POST['contacter'],'category_id'=>$key[2],'version_id'=>$_POST[$ver][$i],'count'=>$_POST[$count][$i],'r_codes'=>$_POST['codes'],'createtime'=>time());
        	             $arReceiveIn = array('mod_id'=>$key[1],'contacter'=>$_POST['contacter'],'current_version'=>$c_v[0]->name,'category_id'=>$key[2],'version_id'=>$_POST[$ver][$i],'count'=>$_POST[$count][$i],'codes'=>$_POST['codes'],'createtime'=>time(),'receivetime'=>$_POST['receivetime']);
        	             $this->inventory_manage($arReceiveIn);
      //查找最新版次，插入receive  	             
        	             $newest_version = $this->version->get_new_version(array('category_id'=>$key[2]));
        	             $arReceiveIn['first_version']=$newest_version[0]->name;
        	             $arReceiveIn['first_version_id']=$newest_version[0]->version_id;
        	             $r_id = $this->receive->insert($arReceiveIn);
        	             $mod = $this->models->select($key[1]);
        	             $category = $category = $this->category->select(array('category_id'=>$ar['category_id']));
        	             $ar['s_r_id'] = $r_id;
   
        	             $ar['mod'] =$mod [0]->name;
        	             $ar['category'] =$category [0]->name;
        	             $version = $this->version->select(array('version_id'=>$ar['version_id']));
        	             $ar['version'] = $version[0]->name;
        	             $ar['type'] = 2;
        	             $ar['is_update']=0;
        	             $ar['receivetime'] = $_POST['receivetime'];
        	             $hs = $this->historys->insert($ar);
        	          }
        	       }
        	    }
        	}   

		  redirect('administrator/receive');

        }else{
          $this->top();
          $data['version'] = $this->version->select('');
          $data['mod'] = $this->models->select('');
          $data['category'] = $this->category->select('');
		  $this->load->view('receive_add',$data);
        }
     }
   
//修改型号资料
    function receive_edit(){
      $id = $this->uri->segment(3);
	  if($_POST){
	  	echo '<pre>';
	  	print_r($_POST);exit;
	  	 $this->historys->receive_update($id);
	     $resu = $this->receive->update($id); 
	     if($resu){
	      $pre_count = $_POST['pre_count'];
	      $after_count = $_POST['count'];
	      $min = $after_count-$pre_count; 
	      $array = array('mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id'],'version_id'=>$_POST['version_id'],'count'=>$min);	
          $this->inventory_manage($array);
	      redirect('administrator/receive');
	  	 }
	  }else{
	     
	     $receive = $this->receive->select($id);
	     $data['receive'] = $receive[0];
	     $data['id']=$id;
	     $this->top();
	     $data['category'] = $this->category->select('');
	     $data['admin'] = $this->admin->login(array('role'=>0));
	     $data['mod'] = $this->models->select('');
	     $data['version'] = $this->version->getversion_bycategory_id($receive[0]->category_id);
	     $this->load->view('receive_edit',$data);
	  }
    }

//删除型号资料	
	function receive_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->receive->del($id); 
	     if($resu){
	  	  redirect('administrator/receive');
	  	 }
	}

//查看型号资料详情
    function receive_detail(){
        $id = $this->uri->segment(3);
	    $receive = $this->receive->select($id);
	    $data['receive'] = $receive[0]; 
	    $data['mod'] = $this->models->select('');
	    $data['admin'] = $this->admin->select('');
	    $data['category'] = $this->category->select('');
	    $data['version'] = $this->version->select('');
	    $this->top();
        $this->load->view('receive_detail',$data);
    }	

    
	
//型号资料列表
    function send(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('send_mod_id',$_POST['mod_id']);
	    	$this->session->set_userdata('send_category_id',$_POST['category_id']);
	    	$this->session->set_userdata('send_company_id',$_POST['company_id']);
	    	$this->session->set_userdata('send_codes',$_POST['codes']);
	    }
	    $mod_id=$this->session->userdata('send_mod_id');
	    $category_id=$this->session->userdata('send_category_id');
	    $company_id=$this->session->userdata('send_company_id');
	    $codes=$this->session->userdata('send_codes');
	    $where = array();
	    
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id);
	    	if($category_id!=""){
	    	  $where['category_id'] = $category_id;
	    	}
	    	if($codes!=''){
	    	  $where['s_codes'] = $codes;
	    	}
	    }
    	 if($company_id!=""){
	    	  $where['company_id'] = $company_id;
	     }	    	
	     $where['type']='1';
	    $data['mod_id'] = $mod_id;
	    $data['category_id'] = $category_id;
	    $data['company_id'] = $company_id;
	    $data['codes'] = $codes;
        $data['mod'] = $this->models->select('');
        $data['category'] = $this->category->select('');
        $data['version'] = $this->version->select('');
        $data['company'] = $this->company->select('');
        $data['admin'] = $this->admin->select('');
	    $total = $this->historys->select($where);
	    $config['base_url'] =  site_url('administrator/send/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['send'] = $this->historys->select($where,$config['per_page'],$page);
		$this->load->view('send',$data);
    }	
    
//型号资料添加
     function send_add(){
     	$user = $this->session->userdata('user');
        if($_POST){
            $company_id = $_POST['company_id'];
            $user_id = $_POST['contacter'];
        foreach($_POST as $k=>$val){
        	
        	    $key = explode('_', $k);
        	    if(count($key)==3){
        	    	
        	       if($key[0]=="version"){	
        	          $ver = $key[0]."_".$key[1]."_".$key[2];
        	          $co = "counts"."_".$key[1]."_".$key[2];
        	          foreach ($_POST[$ver] as $k=>$v){
        	          	    $category_id = $key[2];
        	          	    $mod_id = $key[1];
        	          	    $version_id = $v;
        	          	    $a = $_POST[$co][$k];
        	          //查找库存	    
        	          	    $array=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id);
        	          	    $resu = $this->inventory->select($array);
        	          	    if(count($resu)==0){
        	          	    	$count=0;
        	          	    }else{
        	          	    	$count = $resu[0]->count;
        	          	    }
        	          	    $arr = array('count'=>$count-$a);
        	          	    $result = $this->inventory->update($resu[0]->inventory_id,$arr);
        	          	    if($result){
        	          	    	$arr = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id,'company_id'=>$company_id,'codes'=>$_POST['codes']);
        	          	    	$arra = $this->send->select_by_array($arr);
        	          	    	$count_record = count($arra);
        	          	    	
        	          	    	$cc = $this->receive->select_by_array(array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id));
        	          	    	$b = array('mod_id'=>$mod_id,'user'=>$user_id,'category_id'=>$category_id,'version_id'=>$version_id,'company_id'=>$company_id,'codes'=>$_POST['codes'],'count'=>$a,'createtime'=>time());
        	          	    	$send_b = array('mod_id'=>$mod_id,'user_id'=>$user_id,'category_id'=>$category_id,'version_id'=>$version_id,'company_id'=>$company_id,'codes'=>$_POST['codes'],'count'=>$a,'sendtime'=>$_POST['sendtime'],'createtime'=>time());
        	          	    	if($count_record!=0){
        	          	    		$c = $arra[0]->count+$a;
        	          	    		$r = $this->send->update($arra[0]->send_id,array('count'=>$c));
        	          	    		$s_id = $arra[0]->send_id;
        	          	    		 
        	          	    	}else{
        	          	    		//不存在发放记录
        	          	    		$s_id = $this->send->insert($send_b);
        	          	    	}
        	          	    	$b['s_r_id'] = $s_id;
        	          	    	unset($b['codes']);
        	          	    	$mod = $this->models->select($b['mod_id']);
        	          	    	$category = $this->category->select(array('category_id'=>$b['category_id']));
        	          	    	$version = $this->version->select(array('version_id'=>$b['version_id']));
        	          	    	$b['mod']=$mod[0]->name;
        	          	    	$b['category']=$category[0]->name;
        	          	    	$b['version']=$version[0]->name;
        	          	    	$b['type']=1;
        	          	    	$b['s_codes']=$_POST['codes'];
        	          	    	$b['sendtime']=$_POST['sendtime'];
        	          	    	$this->historys->insert($b);
        	          	    	$update['sendtime']=time();
        	          	    	$this->category->update_array($category_id,$update);
        	          	    }
        	          }
        	       }
        	    }
        	} 
        	redirect('administrator/send');
       
		 
        }else{
          $this->top();
          $data['mod'] = $this->models->select('');
	      $data['category'] = $this->category->select('');
          $data['version'] = $this->version->select('');
          $data['company'] = $this->company->select('');
		  $this->load->view('send_add',$data);
        }
     }
   
//修改型号资料
    function send_edit(){
      $id = $this->uri->segment(3);

	  if($_POST){
	     $mod_id = $_POST['mod_id'];
         $category_id = $_POST['category_id'];
         $version_id = $_POST['version_id'];
         $pre_count = $_POST['pre_count'];
         $pre_version = $_POST['pre_version'];
         $a = $_POST['count'];
         $codes = $_POST['codes'];
    //取当前库存，判断     
         $array=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id);
         $resu = $this->inventory->select($array);
         if(count($resu)==0){
           $count=0;
         }else{
           $count = $resu[0]->count;
         }
                    
           $cc = $pre_count-$a;
           $array['count']=$cc;
           $result = $this->inventory_manage($array);
  //低版本回收
           $arr=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$pre_version,'count'=>'-'.$cc);     
           $this->inventory_manage($arr);  
           
           if($result){
           	   unset($array['count']);
	           $array['des'] = $_POST['des'];
	           $array['user'] = $_POST['user_id'];
	           $array['createtime'] = time();
	           $array['s_codes'] = $codes;
	           $this->historys->send_update($id,$array);
	           unset($array['user']);
	           unset($array['s_codes']);
	           unset($array['createtime']);
	           $array['codes'] = $codes;
	           $array['user_id'] = $_POST['user_id'];
	           $array['updatetime'] = date('Y.m.d');
	           $resu =$this->send->update($id,$array);	          
           }
       
	     if($resu){
	  	  redirect('administrator/send');
	  	 }
	  }else{
	     
	     $send = $this->send->select($id);

	     $data['send'] = $send[0];
	     $data['id']=$id;
	     $this->top();
	     $data['mod'] = $this->models->select('');
	     $data['category'] = $this->category->select('');
	//根据mod，category 查询version     
	     $arr = array('mod_id'=>$send[0]->mod_id,'category_id'=>$send[0]->category_id);
	     
	     $data['version'] = $this->version->getversion_array($arr);
	  
	     $data['company'] = $this->company->select('');
	     $arr['version_id'] = $send[0]->version_id;
	     $inventory = $this->inventory->select_by_array($arr);
	     if(count($inventory)==0){
	     	$data['inventory_count'] = 0;
	     }else{
	     	$data['inventory_count'] = $inventory[0]->count;
	     }
	     
	     $this->load->view('send_edit',$data);
	  }
    }

//删除型号资料	
	function send_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->send->del($id); 
	     if($resu){
	  	  redirect('administrator/send');
	  	 }
	}

//查看型号资料详情
    function send_detail(){
        $id = $this->uri->segment(3);
	    $send = $this->send->select($id);
	    $data['send'] = $send[0]; 
	    $data['mod']= $this->models->select($send[0]->mod_id);
	    $data['category']= $this->category->select(array('category_id'=>$send[0]->category_id));
	    $data['version'] = $this->version->select('');
	    $data['company'] = $this->company->select('');
	    $this->top();
        $this->load->view('send_detail',$data);
    }	
	
	
	
	function company()
	{  
	    $this->top();
	    $total = $this->company->select('');
	    $config['base_url'] =  site_url('administrator/company/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['company_del']=$this->session->userdata('company_del');
		$data['company'] = $this->company->select('',$config['per_page'],$page);
		$this->load->view('company',$data);
	}

//添加发放单位	
	function company_add(){
	  if($_POST){
	  	$resu = $this->company->insert();
	  	if($resu){
	  	  redirect('administrator/company');
	  	}
	  }else{	  	
	    $this->top();
		$this->load->view('company_add');
	  }
	}

//修改发放单位	
	function company_edit(){
	  $id = $this->uri->segment(3);
	  if($_POST){
	     $resu = $this->company->update($id); 
	     if($resu){
	  	  redirect('administrator/company');
	  	 }
	  }else{
	     
	     $company = $this->company->select($id);
	     $data['company'] = $company[0];
	     $data['id']=$id;
	     $this->top();
	     $this->load->view('company_add',$data);
	  }
	}

//删除发放单位
	function company_del(){
		$id = $this->uri->segment(3);
		$companys = $this->historys->select_by_array(array('company_id'=>$id));
		if(count($companys)!=0){

		  $this->session->set_userdata('company_del',1);
		  redirect('administrator/company');exit;
		}else{
	      $resu = $this->company->del($id); 
		}
	     if($resu){
	  	  redirect('administrator/company');
	  	 }
	}
	
	
//查看发放单位详情
    function company_detail(){
        $id = $this->uri->segment(3);
	    $company = $this->company->select($id);
	    $data['company'] = $company[0]; 
	    $this->top();
        $this->load->view('company_detail',$data);
    }	
    	
	
	function index()
	{   
	    $this->top();
	    $total = $this->models->select('');
	    $config['base_url'] =  site_url('administrator/index/');
	   
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['mod_del']=$this->session->userdata('mod_del');
		$this->session->unset_userdata('mod_del');
		$data['mod'] = $this->models->select('',$config['per_page'],$page);
		$this->load->view('model',$data);
	}

//添加型号	
	function model_add(){
		$this->session->set_userdata('mod_del',0);
	  if($_POST){
	  	$resu = $this->models->insert();
	  	if($resu){
	  	  redirect('administrator/index');
	  	}
	  }else{	  	
	    $this->top();
		$this->load->view('model_add');
	  }
	}

//修改型号	
	function model_edit(){
	  $id = $this->uri->segment(3);
	  $this->session->set_userdata('mod_del',0);
	  if($_POST){
	     $resu = $this->models->update($id); 
	     if($resu){
	  	  redirect('administrator/index');
	  	 }
	  }else{
	     
	     $mod = $this->models->select($id);
	     $data['mod'] = $mod[0];
	     $data['id']=$id;
	     $this->top();
	     $this->load->view('model_add',$data);
	  }
	}

//删除型号	
	function model_del(){
		$id = $this->uri->segment(3);
		$re = $this->category->select(array('mod_id'=>$id));
		if(count($re)!=0){
		  $this->session->set_userdata('mod_del',1);
		  redirect('administrator/index');exit;
		}else{
		  $this->session->set_userdata('mod_del',0);
		}
	    $resu = $this->models->del($id); 
	     if($resu){
	  	  redirect('administrator/index');
	  	 }
	}
	
	
//查看设备详情
    function model_detail(){
        $id = $this->uri->segment(3);
	    $mod = $this->models->select($id);
	    $data['mod'] = $mod[0]; 
	    $this->top();
        $this->load->view('model_detail',$data);
    }	
	
    
//设备类别列表    
	function category()
	{  
	    $this->top();
	    if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('category_mod_id',$_POST['mod_id']);
	    }
	    $mod_id=$this->session->userdata('category_mod_id');
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id);
	    }else{
	        $where = "";
	    }
	    	    	
	    $data['mod_id'] = $mod_id;
	    $data['mod'] = $this->models->select('');

		$total = $this->category->select($where);
	    $config['base_url'] =  site_url('administrator/category/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['category_del']=$this->session->userdata('category_del');
		$data['category'] = $this->category->select($where,$config['per_page'],$page);
		$this->load->view('category',$data);
	}

//添加设备类别	
	function category_add(){
		$this->session->set_userdata('category_del',0);
	  if($_POST){
	  	$resu = $this->category->insert();
	  	if($resu){
	  	  redirect('administrator/category');
	  	}
	  }else{	  	
	    $this->top();
	    $data['mod'] = $this->models->select('');
		$this->load->view('category_add',$data);
	  }
	}

//修改设备类别	
	function category_edit(){
	  $id = $this->uri->segment(3);
	  $this->session->set_userdata('category_del',0);
	  if($_POST){
	     $resu = $this->category->update($id); 
	     if($resu){
	  	  redirect('administrator/category');
	  	 }
	  }else{
	     
	     $category = $this->category->select(array('category_id'=>$id));
	     $data['category'] = $category[0];
	     $data['id']=$id;
	     $this->top();
	     $data['mod'] = $this->models->select('');
	     $this->load->view('category_add',$data);
	  }
	}

//删除设备类别	
	function category_del(){
		$id = $this->uri->segment(3);
	    $re = $this->version->select(array('category_id'=>$id));
		if(count($re)!=0){
		  $this->session->set_userdata('category_del',1);
		  redirect('administrator/category');exit;
		}else{
		  $this->session->set_userdata('category_del',0);
		}
		
	    $resu = $this->category->del($id); 
	     if($resu){
	  	  redirect('administrator/category');
	  	 }
	}
	
	
//查看设备类别详情
    function category_detail(){
        $id = $this->uri->segment(3);
	    $category = $this->category->select(array('category_id'=>$id));
	    $data['category'] = $category[0]; 
	    $data['mod'] = $this->models->select('');
	    $this->top();
        $this->load->view('category_detail',$data);
    }	

    
//型号资料列表
    function version(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('version_mod_id',$_POST['mod_id']);
	    	$this->session->set_userdata('version_category_id',$_POST['category_id']);
	    }
	    $mod_id=$this->session->userdata('version_mod_id');
	    $category_id=$this->session->userdata('version_category_id');
	    if($mod_id!=""){
	        if($category_id!=''){
	    	  $where = array('version.mod_id'=>$mod_id,'version.category_id'=>$category_id);
	    	}else{
	    	  $where = array('version.mod_id'=>$mod_id);
	    	}
	    }else{
	        $where = "";
	    }
	    	    	
	    $data['mod_id'] = $mod_id;
	    $data['category_id'] = $category_id;
        $data['category'] = $this->category->select('');
	    $total = $this->version->select2($where);
	    $config['base_url'] =  site_url('administrator/version/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['mod'] = $this->models->select('');
		$version = $this->version->select2($where,$config['per_page'],$page);
		$last_version = $this->version->get_last_verison();
		for($i=0;$i<count($version);$i++){
		     $mod_id = $version[$i]->mod_id;
		     $category_id = $version[$i]->category_id;
		     $version_id = $version[$i]->version_id;
		     $count_send = $this->historys->getlast($category_id,$version_id);
		     $count_receive = $this->historys->getlast($category_id,$version_id);
		     if($count_receive[0]->count==''){ $count = 0;}else{ $count = $count_receive[0]->count;}
		     $version[$i]->count = $count;
	//取出库存和已发放总分数     
	         $c = $this->inventory->select_by_array(array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id));
	         if(count($c)==0){$v_count = 0;}else{$v_count=$c[0]->count;}
	         $version[$i]->inventory = $v_count;
		}
		

		$data['version_del']=$this->session->userdata('version_del');
		$this->session->unset_userdata('version_del');
		$data['version']=$version;
		$data['last_version'] = $last_version;
		$this->load->view('version',$data);
    }	
    
//型号资料添加
     function version_add(){
        if($_POST){
          $result = $this->version->insert();
	        if($result){
		  	  redirect('administrator/version');
		  	}
        }else{
          $this->top();
          $data['mod'] = $this->models->select('');
          $data['category'] = $this->category->select('');
		  $this->load->view('version_add',$data);
        }
     }
   
//修改型号资料
    function version_edit(){
      $id = $this->uri->segment(3);
	  if($_POST){
	  	 $da = array(
	  	          //    'mod_id'=>$_POST['mod_id'],
	  	              'des'=>$_POST['des'],
	  	          //    'category_id'=>$_POST['category_id'],
	  	              'name'=>$_POST['name'],
	  	             );
	     $resu = $this->version->update($da,$id); 
	     if($resu){
	  	  redirect('administrator/version');
	  	 }
	  }else{
	     
	     $version = $this->version->select(array('version_id'=>$id));
	     $data['version'] = $version[0];
	     $data['id']=$id;
	     $this->top();
	     $data['mod'] = $this->models->select('');
	     $data['category'] = $this->category->select('');
	     $this->load->view('version_add',$data);
	  }
    }

//删除型号资料	
	function version_del(){
		$id = $this->uri->segment(3);
//只要存在该版本的接收和发放记录，则不可以删除
        $sends = $this->send->select_by_array(array('version_id'=>$id));
        $receives = $this->receive->select_by_array(array('version_id'=>$id));
        $total = count($sends)+count($receives);
	   if($total!=0){
		  $this->session->set_userdata('version_del',1);
		  redirect('administrator/version');exit;
		}else{
		  $this->session->set_userdata('version_del',0);
		}
	    $resu = $this->version->del($id); 
	     if($resu){
	  	  redirect('administrator/version');
	  	 }
	}

//查看型号资料详情
    function version_detail(){
        $id = $this->uri->segment(3);
	    $version = $this->version->select(array('version_id'=>$id));
	    $data['mod'] = $this->models->select('');
	    $data['version'] = $version[0]; 
	    $data['category'] = $this->category->select('');
	    $this->top();
        $this->load->view('version_detail',$data);
    }	

    
//成套资料列表
    function complete(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('category_mod_id',$_POST['mod_id']);
	    }
	    $mod_id=$this->session->userdata('category_mod_id');
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id);
	    }else{
	        $where = "";
	    }
	    	    	
	    $data['mod_id'] = $mod_id;
	    $data['mod'] = $this->models->select('');
        $data['category'] = $this->category->select('');
	    $total = $this->complete->select($where);
	    $config['base_url'] =  site_url('administrator/complete/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 40; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['complete'] = $this->complete->select($where,$config['per_page'],$page);
		$this->load->view('complete',$data);
    }	
    
//成套资料添加
     function complete_add(){
        if($_POST){
          $result = $this->complete->insert();
	      echo $result;exit;
        }else{
          $this->top();
          $data['category'] = $this->category->select('');
          $data['mod'] = $this->models->select('');
		  $this->load->view('complete_add',$data);
        }
     }
   
//修改成套资料
    function complete_edit(){
      $id = $this->uri->segment(3);
	  if($_POST){
	     $resu = $this->complete->update($_POST['complete_id']); 
	    
	  }else{
	     
	     $complete = $this->complete->select(array('complete_id'=>$id));
	     $data['complete'] = $complete[0];
	     $data['id']=$id;
	     $this->top();
	     $data['mod'] = $this->models->select('');
	     $mod_id = $complete[0]->mod_id;
	     $category_select =  $complete[0]->category_ids;
	     $category_ar = explode(',', $category_select);
	     $categorys = $this->category->select(array('mod_id'=>$mod_id));
	     $result = $this->category->getcategory_bymod_id($mod_id);
	     $count = count($categorys);
	     $str="";
	     if($count!=0){
	         for($i=0;$i<$count;$i++){
	           $str.="<p style='width:190px;float:left;'><input type='checkbox' name='category_ids[]' value='".$categorys[$i]->category_id."' ";
               if(in_array($categorys[$i]->category_id,$category_ar)){
                 $str.=" checked=true ";
               }
	           $str.=" />".$categorys[$i]->name." </p>";
	         }
	       $data['category'] = $str;      
	     }
	     $this->load->view('complete_add',$data);
	  }
    }

//删除成套资料	
	function complete_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->complete->del($id); 
	     if($resu){
	  	  redirect('administrator/complete');
	  	 }
	}

//查看成套资料详情
    function complete_detail(){
        $id = $this->uri->segment(3);
	    $complete = $this->complete->select(array('complete_id'=>$id));
	    $data['complete'] = $complete[0]; 
	    $data['mod'] = $this->models->select($complete[0]->mod_id);
	    $data['category'] = $this->category->select('');
	    $this->top();
        $this->load->view('complete_detail',$data);
    }	 

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */